package com.paschino.utils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.web.multipart.MultipartFile;

import com.paschino.bean.AccountBean;
import com.paschino.bean.AlbumBean;
import com.paschino.bean.CategoryBean;
import com.paschino.bean.ProductBean;
import com.paschino.modal.Album;
import com.paschino.modal.Category;
import com.paschino.modal.Product;
import com.paschino.modal.StoreImage;
import com.paschino.modal.User;
import com.paschino.service.CategoryService;
import com.paschino.service.ImageService;
import com.paschino.service.ProductService;
import com.paschino.service.UserService;

public class ControllerService {

	public static Category prepareCategory(CategoryBean categoryBean,
			String userName) {
		Category category = new Category();
		category.setId(null);
		category.setName(categoryBean.getName());
		category.setDescription(categoryBean.getDescription());
		category.setIsActive(true);
		category.setProcessDate(Formater.dateToString(new Date()));
		category.setUser(userName);
		return category;
	}

	public static List<CategoryBean> prepareCategoryList(
			List<Category> categoryList) {
		List<CategoryBean> categoryBeans = new ArrayList<CategoryBean>();
		for (Category category : categoryList) {
			CategoryBean bean = new CategoryBean();
			bean.setId(category.getId());
			bean.setName(category.getName());
			bean.setDescription(category.getDescription());
			bean.setProcessDate(category.getProcessDate());
			bean.setIsActive(category.getIsActive());
			bean.setUser(category.getUser());
			categoryBeans.add(bean);
		}
		return categoryBeans;
	}

	public static boolean processAddProduct(ProductBean productBean,
			String userName, CategoryService categoryService,
			ProductService productService) {
		Product product = new Product();
		product.setId(null);
		product.setName(productBean.getName());
		product.setPrice(productBean.getPrice());
		product.setDescription(productBean.getDescription());
		product.setIsActive(true);
		product.setIsMain(productBean.getIsMainProduct());
		product.setImage(productBean.getImage());
		product.setCategoryId(categoryService.getCategory(productBean
				.getCategoryId()));
		product.setProcessDate(Formater.dateToString(new Date()));
		product.setUser(userName);

		return productService.addProduct(product);
	}

	public static Product prepareUpdateProduct(ProductBean productBean,
			String userName, CategoryService categoryService,
			ProductService productService) {
		Product product = productService.getProduct(productBean.getId());
		product.setName(productBean.getName());
		product.setDescription(productBean.getDescription());
		product.setPrice(productBean.getPrice());
		product.setIsActive(productBean.getIsActive());
		product.setIsMain(productBean.getIsMainProduct());
		product.setCategoryId(categoryService.getCategory(productBean
				.getCategoryId()));
		product.setProcessDate(Formater.dateToString(new Date()));
		product.setUser(userName);

		return product;
	}

	public static boolean processAddImage(MultipartFile image, String location,
			String folder) throws Exception {
		return FileUpload.saveImage(location, image, folder);
	}

	public static boolean processSaveProductImage(MultipartFile file,
			ImageService imageService) throws Exception {
		StoreImage image = new StoreImage();
		image.setId(null);
		image.setName(file.getOriginalFilename());
		image.setData(file.getBytes());
		image.setImageType(Constants.PRODUCT);
		return imageService.addImage(image);
	}

	public static boolean processChangePassword(AccountBean accountBean,
			String userName, UserService userService) {
		User user = userService.getUser(userName);
		if (accountBean.getNewPass().equals(accountBean.getConfirmPass())) {
			if (MD5.crypt(accountBean.getOldPass()).equals(user.getPassword())) {
				user.setPassword(MD5.crypt(accountBean.getNewPass()));
				return userService.updateUser(user);
			}
		}
		return false;
	}

	public static CategoryBean prepareCategoryBean(String cateId,
			CategoryService categoryService) {
		CategoryBean categoryBean = new CategoryBean();
		Category category = categoryService.getCategory(Integer
				.parseInt(cateId));
		if (null != category) {
			categoryBean.setId(category.getId());
			categoryBean.setName(category.getName());
			categoryBean.setDescription(category.getDescription());
			categoryBean.setIsActive(category.getIsActive());
		}
		return categoryBean;
	}

	public static Category prepareUpdateCategory(CategoryBean categoryBean,
			String userName, CategoryService categoryService) {
		Category category = categoryService.getCategory(categoryBean.getId());
		category.setName(categoryBean.getName());
		category.setDescription(categoryBean.getDescription());
		category.setIsActive(categoryBean.getIsActive());
		category.setProcessDate(Formater.dateToString(new Date()));
		category.setUser(userName);
		return category;
	}

	public static List<ProductBean> prepareListProductBean(
			List<Product> productList) {
		List<ProductBean> list = new ArrayList<ProductBean>();
		for (Product p : productList) {
			ProductBean b = new ProductBean();
			b.setId(p.getId());
			b.setName(p.getName());
			b.setPrice(p.getPrice());
			b.setDescription(p.getDescription());
			b.setProcessDate(p.getProcessDate());
			b.setUser(p.getUser());
			b.setIsActive(p.getIsActive());
			b.setImage(p.getImage());
			b.setIsMainProduct(p.getIsMain());
			b.setCategoryId(p.getCategoryId().getId());
			list.add(b);
		}
		return list;
	}

	public static ProductBean prepareProductBean(String productId,
			ProductService productService) {
		ProductBean productBean = new ProductBean();
		Product product = productService
				.getProduct(Integer.parseInt(productId));
		if (null != product) {
			productBean.setId(product.getId());
			productBean.setName(product.getName());
			productBean.setPrice(product.getPrice());
			productBean.setDescription(product.getDescription());
			productBean.setImage(product.getImage());
			productBean.setProcessDate(product.getProcessDate());
			productBean.setUser(product.getUser());
			productBean.setIsActive(product.getIsActive());
			productBean.setIsMainProduct(product.getIsMain());
			productBean.setCategoryId(product.getCategoryId().getId());
		}
		return productBean;
	}

	public static List<AlbumBean> prepareAlbumBeanList(List<Album> albumList) {
		List<AlbumBean> list = new ArrayList<AlbumBean>();
		for (Album album : albumList) {
			AlbumBean bean = new AlbumBean();
			bean.setId(album.getId());
			bean.setName(album.getName());
			bean.setDescription(album.getDescription());
			bean.setProcessDate(album.getProcessDate());
			bean.setUser(album.getUser());
			bean.setIsActive(album.getIsActive());

			List<String> images = new ArrayList<String>();
			List<StoreImage> imageList = album.getStoreImages();
			if (null != imageList && !imageList.isEmpty()) {
				for (StoreImage storeImage : imageList) {
					String imageName = storeImage.getName();
					images.add(imageName);
				}
			}
			bean.setImageList(images);
			list.add(bean);
		}
		return list;
	}
}
